--[[

Copyright (c) 2012-2013 baby-bus.com

http://www.babybus.com/superdo/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

]]

---------------
-- 全局对象
---------------
box                 = box or {}
box.audio           = box.audio or {}









----------------------
-- 业务逻辑[休息]
----------------------
box.audio.BASE_DIR  = "res/snd/g/"

----------------------
-- 全局变量
----------------------
GLOBAL_NO_WIFI_SOUND = nil


---------------
-- 路径
---------------
-- 获得声音路径
function box.audio.path(somePath)
    return IO.fullPath(box.audio.BASE_DIR .. somePath)
end


---------------
-- 基础方法
---------------
-- 播放音效[通用]
function box.audio.sound(somePath)
    return sound.playSound(box.audio.path(somePath), false, false)
end
-- 停止音效[通用]
function box.audio.unsound(somePath)
    sound.unloadSound(box.audio.path(somePath))
end
-- 播放音乐[通用]
function box.audio.music(somePath)
    return sound.switchMusic(box.audio.path(somePath), true, true)
end
-- 停止音乐[通用]
function box.audio.unmusic(somePath)
    -- Nothing to do!! No API now :) Hah!
end


---------------
-- 业务逻辑[公共]
---------------
-- 播放音效[点击]
function box.audio.soundTap()
    return box.audio.sound("tap.mp3")
end


---------------
-- 无wifi
---------------
-- 播放音效[没有wifi]
function box.audio.soundNoWifi(node)
    if BB_BOOL_NO_WIFI_SOUND_ISPLAYING then
        return 
    end
    local lang = device.language
    box.audio.stopSoundNoWifi()
    if node and node.closeMe then
        node:stop()
        node:closeMe()
    end
    -- 播放音效
    if lang == "zh" or lang == "zht" then 
        BB_BOOL_NO_WIFI_SOUND_ISPLAYING = true
        F.delayCall(5,function ( ... )
            BB_BOOL_NO_WIFI_SOUND_ISPLAYING = false
        end)
        if lang == "zh" then
            NV.showToast("wifi未连接，请检查网络设置")
        else
            NV.showToast("wifi未連接，請檢查網絡設置")
        end
        GLOBAL_NO_WIFI_SOUND = box.audio.sound("nowifi_zh.mp3")
        return GLOBAL_NO_WIFI_SOUND
    end
end


function box.audio.soundNoInternet(node)

    if BB_BOOL_NO_WIFI_SOUND_ISPLAYING then
        return 
    end
    local lang = device.language
    box.audio.stopSoundNoWifi()

    local currenscene = D.getRunningScene()
    if not tolua.isnull(currenscene.accompany_) then
        currenscene.accompany_:qiqiNoInternet()
    else
        -- 播放音效
        if lang == "zh" or lang == "zht" then 
            BB_BOOL_NO_WIFI_SOUND_ISPLAYING = true
            F.delayCall(4,function ( ... )
                BB_BOOL_NO_WIFI_SOUND_ISPLAYING = false
            end)
            if lang == "zh" then
                NV.showToast("wifi未连接，请检查网络设置")
            else
                NV.showToast("wifi未連接，請檢查網絡設置")
            end
            GLOBAL_NO_WIFI_SOUND = box.audio.sound("nowifi_we_re.mp3")
            return GLOBAL_NO_WIFI_SOUND
        end
    end
end


-- 停止无wifi音效
function box.audio.stopSoundNoWifi()
    -- 停止之前的音效
    if GLOBAL_NO_WIFI_SOUND then 
        sound.stopSound(GLOBAL_NO_WIFI_SOUND)
        GLOBAL_NO_WIFI_SOUND = nil
    end
end